<!--
  - Copyright (c) 2018.
  - 北京九思金信科技有限公司对该文件内容保留所有权利。
  - 未经许可，不得私自拷贝、传播、发布、引用该文件内容，违者将被追究法律责任。
  -->

<template>
    <div>
        <span v-if="!editing">{{template.itemName}}</span>
        <el-select  v-if="editing" v-model="template"
                    placeholder="请选择评级模版"
                    value-key="ratingTemplateId"
                    :remote-method="remoteMethod"
                    filterable
                    remote
                    reserve-keyword
                    @change="change"
                    :disabled="disabled">
            <el-option
                    v-for="item in templateData"
                    :key="item.ratingTemplateId"
                    :label="item.itemName"
                    :value="item">
            </el-option>
        </el-select>
    </div>
</template>

<script>
export default {
    name: "FtRatingTemplate",
    props: {
        editing: {
            type: Boolean,
            default: true
        },
        disabled: {
            type: Boolean,
            default: false
        },
        value: [String, Object]
    },
    mounted(){
        this.ratingTemplateId=this.value
        this.loadAll()
    },
    data(){
        return{
            //评级模版主键
            ratingTemplateId: '',
            //评级模版对象
            template:{},
            //评级模版集合
            templateData:[]
        }
    },
    methods:{
        remoteMethod(query) {
            if (query) {
                let params = {
                    queryParam: query
                }
                this.$http.post('/joys-rest/lss-customer/rating/template', params).then(
                    resp => {
                        this.templateData = resp.body
                    }, resp => {
                        this.$message.error(resp.body)
                    }
                )
            } else {
                this.loadAll()
            }
        },
        //全部加载评级模版
        loadAll() {
            this.$http.post('/joys-rest/lss-customer/rating/template', {}).then(
                resp => {
                    this.templateData = resp.body
                }, resp => {
                    this.$message.error(resp.body)
                }
            )
        },
        //change事件
        change() {
            let template = {}
            template['itemId']=this.template.itemId
            template['ratingTemplateId']=this.template.ratingTemplateId
            template['itemCode']=this.template.itemCode
            template['itemName']=this.template.itemName
            this.$emit('input', template)
            this.$emit('change', template)
        },
    },
}
</script>

<style scoped>

</style>
